package org.jeecg.modules.api.controller.daping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.DictUtil;
import org.jeecg.modules.api.controller.ShippingVo;
import org.jeecg.modules.api.controller.daping.mapper.DaPingMapper;
import org.jeecg.modules.api.controller.daping.vo.HyDaPingVO;

import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.tj.mapper.HyTjMapper;
import org.jeecg.modules.wlhy.tj.vo.HyOrderTj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/api/wlhy/daping")
@Api(tags = "大屏")
@Slf4j
public class DaPingController {


    @Autowired
    private HyTjMapper hyTjMapper;

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Autowired
    private DictUtil dictUtil;
    @Autowired
    private IHySettingService hySettingService;

    @Autowired
    private DaPingMapper daPingMapper;

    @Autowired
    private IHyDriverService driverService;
    @Autowired
    private IHyVehicleService vehicleService;

    @Autowired
    private IHyCustomerService customerService;

    @Autowired
    private IHyShippingNoteService shippingNoteService;


    //
    @GetMapping(value = "/getKeHuPaiMing")
    public Result<?> getKeHuPaiMing() {

        List<HyOrderTj> list1 = hyTjMapper.getKeHuTj("shipping_count");
        List<HyOrderTj> list2 = hyTjMapper.getKeHuTj("goods_weight_loading");

        Map map = new HashMap<>();
        map.put("list1", list1);
        map.put("list2", list2);
        return Result.OK(map);
    }

    @GetMapping(value = "/getYundanList")
    public Result<?> getYundanList() {

        List<HyShippingNote> listJd = hyShippingNoteService.list(new QueryWrapper<HyShippingNote>()
                .eq("shipping_status", "1")
        );
        List<HyShippingNote> listZh = hyShippingNoteService.list(new QueryWrapper<HyShippingNote>()
                .eq("shipping_status", "3")
        );
        List<HyShippingNote> listJh = hyShippingNoteService.list(new QueryWrapper<HyShippingNote>()
                .eq("shipping_status", "4")
        );
        List<HyShippingNote> listQs = hyShippingNoteService.list(new QueryWrapper<HyShippingNote>()
                .eq("shipping_status", "5")
        );

        Map map = new HashMap<>();
        map.put("listJd", dictUtil.parseArrayListDict(listJd));
        map.put("listZh", dictUtil.parseArrayListDict(listZh));
        map.put("listJh", dictUtil.parseArrayListDict(listJh));
        map.put("listQs", dictUtil.parseArrayListDict(listQs));
        return Result.OK(map);
    }

    @GetMapping(value = "/getXianLuList")
    public Result<?> getXianLuList() {
        String[] latestMonth = DateUtils.getLatest12Month().split("=");
        List<ShippingVo> luXianName = hyShippingNoteService.getLuXianName();
        Map map = new HashMap();
        map.put("luXianName", luXianName);

        List<String> latest12MonthList = DateUtils.getLatest12MonthList();
        map.put("latest12MonthList", latest12MonthList);

        for (ShippingVo shippingVo : luXianName) {
            List<ShippingVo> luXian = hyShippingNoteService.getLuXian(latestMonth[0], latestMonth[1], shippingVo.getId());
            String[] str = new String[12];
            for (int i = 0; i < latest12MonthList.size(); i++) {
                for (int i1 = 0; i1 < luXian.size(); i1++) {
                    if (luXian.get(i1).getMonths().equals(latest12MonthList.get(i))) {
                        if (luXian.get(i1).getGoodsWeightLoading() == null) {
                            str[i] = "0";
                        } else {
                            str[i] = luXian.get(i1).getGoodsWeightLoading();
                        }
                    } else {
                        str[i] = "0";
                    }
                }
            }
            shippingVo.setMoney(str);
        }
        return Result.OK(map);
    }

    @GetMapping(value = "/getXianLuList2")
    public Result<?> getXianLuList2() {
        String[] latestMonth = DateUtils.getLatest12Month().split("=");
        List<ShippingVo> luXianName = hyShippingNoteService.getLuXianName();
        Map map = new HashMap();
        map.put("luXianName", luXianName);

        List<String> latest12MonthList = DateUtils.getLatest12MonthList();
        map.put("latest12MonthList", latest12MonthList);

        for (ShippingVo shippingVo : luXianName) {
            List<ShippingVo> luXian = hyShippingNoteService.getLuXian2(latestMonth[0], latestMonth[1], shippingVo.getId());
            String[] str = new String[12];
            for (int i = 0; i < latest12MonthList.size(); i++) {
                for (int i1 = 0; i1 < luXian.size(); i1++) {
                    if (luXian.get(i1).getMonths().equals(latest12MonthList.get(i))) {
                        if (luXian.get(i1).getGoodsWeightLoading() == null) {
                            str[i] = "0";
                        } else {
                            str[i] = luXian.get(i1).getGoodsWeightLoading();
                        }
                    } else {
                        str[i] = "0";
                    }
                }
            }
            shippingVo.setMoney(str);
        }
        return Result.OK(map);
    }


    @GetMapping(value = "/getName")
    public String getDapingZongshuju() {
        String dpName = hySettingService.getSettingValueByTag("web_platform_name");
        JSONObject res = new JSONObject();
        res.put("value", dpName);
        return res.toJSONString();
    }

    @GetMapping(value = "/kehupaiming")
    public String kehupaiming() {

        List<HyDaPingVO> getkehupaiming = daPingMapper.getkehupaiming();

        JSONArray list = new JSONArray();
        for (HyDaPingVO hyDaPingVO : getkehupaiming) {
            JSONObject res = new JSONObject();
            res.put("name", hyDaPingVO.getName());
            res.put("value", hyDaPingVO.getValue());
            list.add(res);
        }
        return list.toJSONString();

    }

    @GetMapping(value = "/kehupaiming2")
    public String kehupaiming2() {
        List<HyDaPingVO> getkehupaiming = daPingMapper.getkehupaimingTop5();

        JSONArray list = new JSONArray();
        for (HyDaPingVO hyDaPingVO : getkehupaiming) {
            JSONObject res = new JSONObject();
            res.put("name", hyDaPingVO.getName());
            res.put("value", hyDaPingVO.getValue());
            res.put("type", "客户排名");
            list.add(res);
        }
        return list.toJSONString();
    }

    @GetMapping(value = "/kehupaimingTop5")
    public String kehupaimingTop5() {
        List<HyDaPingVO> getkehupaiming = daPingMapper.getkehupaimingTop5();
        JSONArray list = new JSONArray();
        for (int i = 0; i < getkehupaiming.size(); i++) {
            JSONObject res = new JSONObject();
            res.put("id", i + 1);
            res.put("title", getkehupaiming.get(i).getName());
            res.put("describe", "运单数:" + getkehupaiming.get(i).getValue());
            list.add(res);
        }
        return list.toJSONString();
    }

    @GetMapping(value = "/getjdyundan")
    public Object[] getjdyundan() {
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat f = new SimpleDateFormat(strDateFormat);

        List<HyShippingNote> listJd = hyShippingNoteService.list(new QueryWrapper<HyShippingNote>()
                .eq("shipping_status", "1")
        );
        List<String[]> list2 = new ArrayList<>();
        for (int i = 0; i < listJd.size(); i++) {
            String[] obj = new String[3];
            JSONObject jsonObject = dictUtil.parseObjectDict(listJd.get(i));
            obj[0] = jsonObject.get("vehicleId_dictText").toString();
            obj[1] = jsonObject.get("driverId_dictText").toString();
            obj[2] = f.format(listJd.get(i).getCreateTime());
            list2.add(obj);
        }
        Object[] objects = list2.toArray();
        return objects;
    }

    @GetMapping(value = "/getzhyundan")
    public Object[] getzhyundan() {
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat f = new SimpleDateFormat(strDateFormat);

        List<HyShippingNote> listJd = hyShippingNoteService.list(new QueryWrapper<HyShippingNote>()
                .eq("shipping_status", "3")
        );
        List<String[]> list2 = new ArrayList<>();
        for (int i = 0; i < listJd.size(); i++) {
            String[] obj = new String[3];
            JSONObject jsonObject = dictUtil.parseObjectDict(listJd.get(i));
            obj[0] = jsonObject.get("vehicleId_dictText").toString();
            obj[1] = jsonObject.get("driverId_dictText").toString();
            obj[2] = f.format(listJd.get(i).getCreateTime());
            list2.add(obj);
        }
        Object[] objects = list2.toArray();
        return objects;
    }

    @GetMapping(value = "/getjhyundan")
    public Object[] getjhyundan() {
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat f = new SimpleDateFormat(strDateFormat);

        List<HyShippingNote> listJd = hyShippingNoteService.list(new QueryWrapper<HyShippingNote>()
                .eq("shipping_status", "4")
        );
        List<String[]> list2 = new ArrayList<>();
        for (int i = 0; i < listJd.size(); i++) {
            String[] obj = new String[3];
            JSONObject jsonObject = dictUtil.parseObjectDict(listJd.get(i));
            obj[0] = jsonObject.get("vehicleId_dictText").toString();
            obj[1] = jsonObject.get("driverId_dictText").toString();
            obj[2] = f.format(listJd.get(i).getCreateTime());
            list2.add(obj);
        }
        Object[] objects = list2.toArray();
        return objects;
    }

    @GetMapping(value = "/getqsyundan")
    public Object[] getqsyundan() {
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat f = new SimpleDateFormat(strDateFormat);

        List<HyShippingNote> listJd = hyShippingNoteService.list(new QueryWrapper<HyShippingNote>()
                .eq("shipping_status", "5")
        );
        List<String[]> list2 = new ArrayList<>();
        for (int i = 0; i < listJd.size(); i++) {
            String[] obj = new String[3];
            JSONObject jsonObject = dictUtil.parseObjectDict(listJd.get(i));
            obj[0] = jsonObject.get("vehicleId_dictText").toString();
            obj[1] = jsonObject.get("driverId_dictText").toString();
            obj[2] = f.format(listJd.get(i).getCreateTime());
            list2.add(obj);
        }
        Object[] objects = list2.toArray();
        return objects;
    }


    @GetMapping(value = "/getTongji")
    public String getTongji() {
        HyDaPingVO count = daPingMapper.getCount();
        JSONArray list = new JSONArray();
        int driverCount = driverService.count();
        int vehicleCount = vehicleService.count();
        int customerCount = customerService.count();
        int shippingNoteCount = shippingNoteService.count();
        JSONObject sjres = new JSONObject();
        sjres.put("backgroundColor", "#409EFF");
        sjres.put("prefixText", "入驻司机");
        sjres.put("value", driverCount);
        sjres.put("suffixText", "人");
        list.add(sjres);

        JSONObject cheres = new JSONObject();
        cheres.put("backgroundColor", "#409EFF");
        cheres.put("prefixText", "入驻车辆");
        cheres.put("value", vehicleCount);
        cheres.put("suffixText", "辆");
        list.add(cheres);

        JSONObject qiyeres = new JSONObject();
        qiyeres.put("backgroundColor", "#409EFF");
        qiyeres.put("prefixText", "入驻企业");
        qiyeres.put("value", customerCount);
        qiyeres.put("suffixText", "个");
        list.add(qiyeres);

        JSONObject yundanres = new JSONObject();
        yundanres.put("backgroundColor", "#409EFF");
        yundanres.put("prefixText", "总运单数");
        yundanres.put("value", shippingNoteCount);
        yundanres.put("suffixText", "单");
        list.add(yundanres);

        JSONObject zzres = new JSONObject();
        zzres.put("backgroundColor", "#409EFF");
        zzres.put("prefixText", "运输总量");
        zzres.put("value", count.getZz());
        zzres.put("suffixText", "吨");
        list.add(zzres);

        JSONObject jeres = new JSONObject();
        jeres.put("backgroundColor", "#409EFF");
        jeres.put("prefixText", "总成交金额");
        jeres.put("value", count.getJe());
        jeres.put("suffixText", "万元");
        list.add(jeres);


        return list.toJSONString();

    }

    @GetMapping(value = "/getShouhuo")
    public String getShouhuo() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int qunian = year - 1;
        List<HyDaPingVO> getshouhuo = daPingMapper.getshouhuo(String.valueOf(year));
        List<HyDaPingVO> getshouhuo2 = daPingMapper.getshouhuo(String.valueOf(qunian));

        JSONArray list = new JSONArray();
        for (HyDaPingVO hyDaPingVO : getshouhuo) {
            JSONObject sjres = new JSONObject();
            sjres.put("name", hyDaPingVO.getName());
            sjres.put("value", hyDaPingVO.getValue() == null ? 0 : hyDaPingVO.getValue());
            sjres.put("type", year);
            list.add(sjres);
        }
        for (HyDaPingVO hyDaPingVO : getshouhuo2) {
            JSONObject sjres = new JSONObject();
            sjres.put("name", hyDaPingVO.getName());
            sjres.put("value", hyDaPingVO.getValue() == null ? 0 : hyDaPingVO.getValue());
            sjres.put("type", qunian);
            list.add(sjres);
        }

        return list.toJSONString();

    }

    @GetMapping(value = "/getShouhuo2")
    public String getShouhuo2() {
        List<HyDaPingVO> getshouhuo = daPingMapper.getshouhuo2();
        JSONArray list = new JSONArray();
        for (HyDaPingVO hyDaPingVO : getshouhuo) {
            JSONObject sjres = new JSONObject();
            sjres.put("name", hyDaPingVO.getName());
            sjres.put("value", hyDaPingVO.getValue() == null ? 0 : hyDaPingVO.getValue());
            list.add(sjres);
        }

        return list.toJSONString();

    }
    @GetMapping(value = "/getShouhuoZhexian")
    public String getShouhuoZhexian() {
        List<HyDaPingVO> getshouhuo = daPingMapper.getshouhuo2Top5();
        JSONArray list = new JSONArray();
        for (HyDaPingVO hyDaPingVO : getshouhuo) {
            JSONObject sjres = new JSONObject();
            sjres.put("name", hyDaPingVO.getName());
            sjres.put("value", hyDaPingVO.getValue() == null ? 0 : hyDaPingVO.getValue());
            sjres.put("type", "1");
            list.add(sjres);
        }

        return list.toJSONString();

    }




}
